package leetcode;

import java.util.HashSet;

public class LeetCode142 {

    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        ListNode node = head;
        while (node != null) {
            if (set.contains(node))
                return node;
            set.add(node);
            node = node.next;
        }
        return null;
    }

    public ListNode detectCycle1(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null) {
            if (slow == fast)
                System.out.println("有环");
            slow = slow.next;
            fast = fast.next.next;
        }
        return null;
    }
//------------------------------------------------------------------
}
